﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Finally - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Finally statement ensures that one or more code lines are always executed after the execution of a Try statement." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Finally</h1>

<p>Ensures that one or more statements (functions or expressions) are always executed after the execution of a <a href="Try.htm">try</a> statement.</p>

<pre class="Syntax"><span class="func">Finally</span> <i>Statement</i></pre>
<pre class="Syntax" style="line-height: 100%"><span class="func">Finally</span>
{
    <i>Statements</i>
}</pre>
<h2 id="Remarks">备注</h2>
<p>Every use of <em>finally</em> must belong to (be associated with) a <a href="Try.htm">try</a> (or <a href="Catch.htm">catch</a>) statement above it. A <em>finally</em> always belongs to the nearest unclaimed <em>try</em> statement above it unless a <a href="Block.htm">block</a> is used to change that behavior.</p>
<p><em>Try</em> statements behave differently depending on whether <em>catch</em> or <em>finally</em> is present. For more information, 请参阅 <a href="Try.htm">Try</a>.</p>
<p><em>Goto</em>, <em>break</em>, <em>continue</em> and <em>return</em> cannot be used to exit a <em>finally</em> block, as that would require suppressing any control flow instructions within the <em>try</em> block. 例如, if <em>try</em> uses <code>return 42</code>, the value 42 is returned after the finally block executes. Attempts to jump out of a <em>finally</em> block using one of these statements are detected as errors at load time where possible, or at run time otherwise.</p>
<p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used with the <em>finally</em> statement. 例如:</p>
<pre>try {
    ...
} finally {
    ...
}

try {
    ...
} catch e {
    ...
} finally {
    ...
}</pre>
<h2 id="Related">相关</h2>
<p><a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, <a href="Throw.htm">Throw</a>, <a href="Block.htm">Blocks</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>try
{
    ToolTip "Working..."
    Example1()
}
catch e
{
    <em>; 关于 e 对象的更多细节, 请参阅 <a href="Catch.htm">Catch</a>.</em>
    MsgBox(&quot;Exception thrown!`n`nwhat: &quot; e.what &quot;`nfile: &quot; e.file
        . &quot;`nline: &quot; e.line &quot;`nmessage: &quot; e.message &quot;`nextra: &quot; e.extra,, 16)
}
finally
{
    ToolTip <em>; 隐藏工具提示</em>
}

MsgBox "Done!"

<em>; 此函数包含了清理代码的 Finally 区块</em>
Example1()
{
    try
        Example2()
    finally
        MsgBox "This is always executed regardless of exceptions"
}

<em>; 当系统分钟数为奇数时此函数执行会报错</em>
Example2()
{
    if Mod(A_Min, 2)
        throw Exception(&quot;Test exception&quot;)
    MsgBox "Example2 did not fail"
}</pre>
</div>

</body>
</html>